chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
	if (request.action == "addBookmark") {
		var dialog = new AddBookmarkDialog();
		dialog.onSuccess = function(cateId, title, url, cateName){
			var checkCateId = function(){
				var dtd = $.Deferred();

				if (cateId == -1) {
					stub.api.callEx("cate/create", {
						"name": cateName
					}).done(function(json){
						if (!json.error && !json.errors) {
							dtd.resolve(json.data.id);
						} else {
							dtd.reject("无法创建新的分类: " + json.status);
						}
					});
				} else {
					dtd.resolve(cateId);
				}
				return dtd.promise();
			};

			checkCateId().done(function(cateId){
				stub.api.callEx("bookmark/create", {
					"cateId" : cateId,
					"title" : title,
					"url" : url
				}).done(function(json){
					if (json.error || json.errors) {
						alert("添加失败: " + json.status);
					} else {
						alert("已添加");
					}
				}).fail(function(error){
					alert("添加失败:" + error.code)
				});
			}).fail(function(error){
				alert(error);
			});
		};
		dialog.show();
	} else if (request.action == "test") {
		alert('test in page');
		stub.api.callEx("qunit/test", {"data" : "hello world"}).done(function(json){
			alert(json.data);
		});
	}
});

function Dialog(){
}

Dialog.prototype = {
	init: function(){
		this.dialog = $("<div class='bm-dialog bookmark-my' style='z-index: 65535;'></div>");
		this.dialog.appendTo("body");
	},
	getContent: function(){
		return $('<p>hello world</p>');
	},
	show: function(){
		this.dialog.empty().append(this.getContent());
		this.dialog.show();
	},
	success: function(){
		if (typeof this.onSuccess == "function") {
			this.onSuccess.apply(this, arguments);
		}
	},
	cancel: function(){
		if (typeof this.onCancel == "function") {
			this.onCancel();
		}
	},
	close: function(){
		this.dialog.hide();
	}
};

function AddBookmarkDialog() {
	Dialog.call(this);
	this.init();
}

AddBookmarkDialog.prototype = $.extend(new Dialog(), {
	getContent: function(){
		var _this = this;

		var title = $("title").text();
		var url = document.location.href;

		var form = $("<div></div>");
		form.append("<div class='form-group'><label>分类：</label><select class='form-control' name='cateId' id='cateId'><option value='0'>未选择</option></select></div>");
		form.append("<div class='form-group' id='partCateName' style='display: none;'><label>分类名：</label><input class='form-control' type='text' name='cateName' id='cateName' value=''></div>");
		form.append("<div class='form-group'><label>标题：</label><input class='form-control' type='text' name='title' id='title' value='"+title+"'></div>");
		form.append("<div class='form-group'><label>地址：</label><input class='form-control' type='text' name='url' id='url' value='"+url+"'></div>");
		form.append("<div class='form-group'><button type='button' class='btn btn-primary' name='submit'>提交</button><button type='button' name='cancel'>取消</button></div>");

		var select = form.find("#cateId");
		var partCateName = form.find("#partCateName");

		stub.api.callEx("cate/list").done(function(json){
			var cates = json.data;
			cates.forEach(function(cate, index){
				select.append("<option value='"+cate.id+"'>"+cate.name+"</option>");
			});
			select.append("<option value='-1'>+新的分类</option>");
		});

		select.on("change", function(){
			if ($(this).val() == -1) {
				partCateName.show();
			} else {
				partCateName.hide();
			}
		});

		form.find("[name='cancel']").on("click", function(){
			_this.cancel();
			_this.close();
		});

		form.find("[name='submit']").on("click", function(){
			var cateId = form.find("#cateId").val();
			var title = form.find("#title").val();
			var url = form.find("#url").val();
			var cateName = form.find("#cateName").val();

			if (cateId == 0) {
				alert('请选择分类');
				return ;
			}

			if (cateId == -1 && cateName.length == 0) {
				alert('请输入新分类名称');
				return ;
			}

			_this.success(cateId, title, url, cateName);
			_this.close();
		});

		return form;
	}
});

